package io.github.tangwan.algorithm;

/**
 * 多线程--按序打印
 * @author tangwan
 * @version V1.0
 * created on 2021/10/8 10:31
 * @since JDK 1.8
 */
public class Question1114 {

    private volatile int flag = -1;

    public Question1114(){

    }

    public void first(Runnable printFirst){
        printFirst.run();
        flag = 1;
    }

    public void second(Runnable printSecond) {
        while (flag != 1){

        }
        printSecond.run();
        flag = 2;
    }

    public void third(Runnable printThird){
        while (flag != 2){

        }
        printThird.run();
    }

    public static void main(String[] args) {
        Question1114 question1114 = new Question1114();
        question1114.first(new PrintFirst());
        question1114.second(new PrintSecond());
        question1114.third(new PrintThird());
        Thread t1 = new Thread();
    }

}

class PrintFirst implements Runnable {

    @Override
    public void run() {
        System.out.println("first");
    }

}

class PrintSecond implements Runnable {

    @Override
    public void run() {
        System.out.println("second");
    }

}

class PrintThird implements Runnable {

    @Override
    public void run() {
        System.out.println("third");
    }

}
